package DMSXL._2024.BinaryTrees;

public class mergeTrees {
    public static void main(String[] args) {
        Integer[] nums1 = {1,3,2,5};
        Integer[] nums2 = {2,1,3,null,4,null,7};
        TreeNode root1 = CreateTree.createBinaryTree(nums1,0);
        TreeNode root2 = CreateTree.createBinaryTree(nums2,0);
        System.out.println(new mergeTrees().mergeTrees(root1,root2).val);
    }
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        //如果有一颗树为空，则立即返回另外一棵树
        if(root1 == null){
            return root2;
        }
        if(root2 == null){
            return root1;
        }
        root1.val += root2.val;
        root1.left = mergeTrees(root1.left,root2.left);
        root1.right = mergeTrees(root1.right,root2.right);
        return root1;
    }

}
